package com.yy.jhl.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.yy.jhl.R;

/** 播放提示音
 * Created by Ellis.Li on 2017/8/7.
 */

public class SoundPoolUtil {
    static Context mContext;
    static SoundPoolUtil instance;
    static SoundPool soundPool;
    public static SoundPoolUtil getInstance(Context cxt){
        mContext = cxt;
        initSoundPool();
        if (instance == null){
            synchronized (SoundPoolUtil.class) {
                if (instance == null){
                    instance = new SoundPoolUtil();
                }
            }
        }
        return instance;
    }
    private static void initSoundPool(){
        soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        soundPool.load(mContext, R.raw.notificationsound, 1);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int i, int i1) {
                if (soundPool != null) {
                    soundPool.play(1, 1.0f, 1.0f, 0, 0, 1.0f);
                }
            }
        });
    }
    public void start(){
        if (soundPool != null) {
            soundPool.play(1, 1.0f, 1.0f, 0, 0, 1.0f);
        }
    }
    public void finish(){
        // 销毁的时候释放SoundPool资源
        if (soundPool != null) {
            soundPool.release();
            soundPool = null;
        }
    }
}
